package com.woniu.crowd.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.woniu.crowd.client.CrowdManageClient;
import com.woniu.crowd.client.param.AddCrowdProductParam;
import com.woniu.crowd.client.param.ListCrowdParam;
import com.woniu.crowd.client.param.PartakeCrowdParam;
import com.woniu.crowd.client.param.upCrowdParam;
import com.woniu.crowd.client.result.CrowdOrderResultInfo;
import com.woniu.crowd.client.result.CrowdfundingProductsResultInfo;
import com.woniu.crowd.common.util.WebUtil;
import com.woniu.crowd.infra.model.CrowdLog;
import com.woniu.crowd.service.CrowdLogService;
import com.woniu.crowd.service.CrowdOrderService;
import com.woniu.crowd.service.CrowdUserReleService;
import com.woniu.crowd.service.CrowdfundingProductsService;
import com.woniu.crowd.service.dto.CrowdOrderDto;
import com.woniu.crowd.service.dto.CrowdfundingProductsDto;
import com.woniu.crowd.service.form.PartakeCrowdForm;
import com.woniu.crowd.service.form.upCrowdForm;
import com.woniu.product.client.ProductManageClient;
import com.woniu.product.client.ProductQueryClient;
import com.woniu.product.client.param.AddProductParam;
import com.woniu.product.client.param.ListCrowdProductParam;
import com.woniu.product.client.param.ListCrowdProductParam;
import com.woniu.product.client.result.SpuResultInfo;
import com.woniu.result.Result;
import com.woniu.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@RestController
@Slf4j
public class CrowdManageApiImpl implements CrowdManageClient {
    @Resource
    private ProductManageClient productManageClient;

    @Resource
    private ProductQueryClient productQueryClient;
    @Resource
    private CrowdUserReleService crowdUserReleService;
    @Resource
    private CrowdfundingProductsService crowdfundingProductsService;

    @Resource
    private CrowdOrderService crowdOrderService;
    @Resource
    private CrowdLogService crowdLogService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public Result addCrowdProduct(AddCrowdProductParam addCrowdProductParam) {
        log.info("新增众筹商品:{}", addCrowdProductParam);
        AddProductParam addProductParam = BeanUtil.copyProperties(addCrowdProductParam, AddProductParam.class);
        addProductParam.setSpuType("CROWD");
        CrowdLog crowdLog = new CrowdLog();
        crowdLog.setLogUser(addCrowdProductParam.getLogUser());
        crowdLog.setLogTime(DateUtil.parseDateLong());
        crowdLog.setLogContnet("新增众筹商品");
        crowdLog.setCrowdId(-1);
        crowdLogService.save(crowdLog);
        return productManageClient.addProduct(addProductParam);
    }

    @Override
    public Result userPartakeCrowd(PartakeCrowdParam partakeCrowdParam) throws Exception {
        log.info("用户参与众筹:{}", partakeCrowdParam);
        PartakeCrowdForm partakeCrowdForm = BeanUtil.copyProperties(partakeCrowdParam, PartakeCrowdForm.class);
        CrowdOrderDto crowdOrderDto = crowdOrderService.userPartakeCrowd(partakeCrowdForm);
        CrowdOrderResultInfo crowdOrderResultInfo = BeanUtil.copyProperties(crowdOrderDto, CrowdOrderResultInfo.class);
        return Result.getInstance(CrowdOrderResultInfo.class).setMsg("参与成功").setCode(200).setData(crowdOrderResultInfo);
    }

    @Override
    public Result manageListCrowd(ListCrowdParam listCrowdParam) {
        log.info("后台分页查询众筹商品:{}", listCrowdParam);
        ListCrowdProductParam listCrowdProductParam = BeanUtil.copyProperties(listCrowdParam, ListCrowdProductParam.class);
        listCrowdProductParam.setSpuType("CROWD");
        return productQueryClient.pageListProductCrowd(listCrowdProductParam);

    }

    @Override
    public Result CrowdSpuUploadFile(MultipartFile file) throws IOException {

        return productManageClient.SPUuploadFile(file);

    }

    @Override
    public void crowdPay(String crowdOrderNum) throws Exception {
        String result = crowdOrderService.crowdPay(crowdOrderNum);
        if (result != null) {
            //支付回查
            Message<String> message = MessageBuilder.withPayload(crowdOrderNum).build();
            rocketMQTemplate.syncSend("LXLG-find-crowd-pay", message, 5000, 5);
            WebUtil.httpServletResponse().setCharacterEncoding("utf8");
            WebUtil.httpServletResponse().setContentType("text/html;charset=utf-8");
            WebUtil.httpServletResponse().getWriter().write(result);
            WebUtil.httpServletResponse().getWriter().flush();
        } else {
            WebUtil.httpServletResponse().sendRedirect("payment-fail.html");
        }
    }

    @Override
    public void crowdPayCallBack() {
        System.out.println("执行支付回调");
        String state = WebUtil.httpServletRequest().getParameter("trade_status");
        if (state != null && state.equals("TRADE_SUCCESS")) {
            String num = WebUtil.httpServletRequest().getParameter("out_trade_no");
            System.out.println("发送消息执行成功逻辑" + num);
            Message<String> message = MessageBuilder.withPayload(num).build();
            rocketMQTemplate.syncSend("LXLG-crowd-order-payok", message);
        }
    }

    @Override
    public Result upCrowd(upCrowdParam upCrowdParam) {
        log.info("上架众筹商品：{}", upCrowdParam);
        upCrowdForm upCrowdForm = BeanUtil.copyProperties(upCrowdParam, upCrowdForm.class);
        return crowdfundingProductsService.upCrowd(upCrowdForm);
    }
}
